package com.demo.base.rabbitmq.queue;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.demo.base.rabbitmq.constant.MqConstant.*;

/**
 * @author 曳戈泰尔
 * @version 1.0
 * @description 队列、交换机、绑定配置 延迟
 * @date 2023/3/10 4:42 PM
 */
@Configuration
public class MqDelayQueue {

    /**
     * 创建延时队列
     *
     * @return
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(DELAY_QUEUE, true);
    }

    /**
     * 创建延时交换机 路由模式 延时消息
     *
     * @return
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定延时消息、交换机、路由
     *
     * @return
     */
    @Bean
    public Binding delayBind() {
        return BindingBuilder.bind(delayQueue())
                .to(delayExchange())
                .with(DELAY_ROUTING_KEY)
                .noargs();
    }
}
